Passed
Push — master ( 0a8095...60f354 )
by hung
01:03
created

output.test.js ➔ ... ➔ ???   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
c 3
b 0
f 0
nc 3
dl 0
loc 8
rs 9.4285
nop 0
1
const fs = require('fs')
2
const { join } = require('path')
3
4
const outputHelper = require('../../helper/output')
5
const TIMEOUT = 1000
6
7
describe('Output helper', () => {
8
  const spyError = jest.spyOn(console, 'error').mockImplementation(() => {})
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {})
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
11
  const file = 'test/output.txt'
12
  const path = join(__dirname, '../../', file)
13
14
  it('output has empty data', () => {
15
    const result = outputHelper('', null)
16
    expect(result).toBe(null)
17
  })
18
19
  it('output is console', () => {
20
    outputHelper(null, 'data')
21
    expect(spyLog).toHaveBeenCalled()
22
    expect(spyLog.mock.calls[0][0]).toContain('data')
23
  })
24
25
  it('output is invalid', () => {
26
    outputHelper(1, 'data')
27
    expect(spyError).toHaveBeenCalled()
28
    expect(spyError.mock.calls[0][0]).toContain('Invalid Output')
29
  })
30
31
  it('output to file', done => {
32
    outputHelper(file, 'data')
33
    setTimeout(() => {
34
      try {
35
        expect(fs.readFileSync(path, 'utf8')).toBe('data')
36
        done()
37
      } catch (err) {
38
        done.fail(err)
39
      }
40
    }, TIMEOUT)
41
  })
42
43
  it('output to not existed file', done => {
44
    const dummy = 'dummy.txt'
45
    outputHelper(dummy, 'data')
46
    setTimeout(() => {
47
      try {
48
        expect(spyError).toHaveBeenCalled()
49
        expect(spyError.mock.calls[1][0]).toContain('Cannot write file to')
50
        done()
51
      } catch (err) {
52
        done.fail(err)
53
      }
54
    }, TIMEOUT)
55
  })
56
57
  it('output to write stream', done => {
58
    const writeable = fs.createWriteStream(path)
59
    outputHelper(writeable, 'data_stream')
60
    setTimeout(() => {
61
      try {
62
        expect(fs.readFileSync(path, 'utf8')).toBe('data_stream\r\n')
63
        done()
64
      } catch (err) {
65
        done.fail(err)
66
      }
67
    }, TIMEOUT)
68
  })
69
})
70